<?php

namespace Wgx\Admin\Entity;

use PhpYes\Apps;
use PhpYes\Entity\BaseEntity;
use Wgx\PdoEx\QueryBuilder;

class AdminUserEntity extends BaseEntity
{
    const AdminRoot = 1; // root管理员拥有所有权限,由登陆后的normal管理员切换进入
    const AdminSpecialBegin = 2; // 特殊管理员的某些权限是在代码中特定实现的,由登陆后的normal管理员切换进入,继承normal管理员的所有权限
    const AdminSpecialEnd = 9;
    const AdminVirtualTemp = 10; // 10规划为临时虚拟管理员,用于辅助管理虚拟管理员和一些临时用途
    const AdminVirtualBegin = 11; // 虚拟管理员不能登陆
    const AdminVirtualEnd = 1000;
    const AdminNormalBegin = 1001; // 普通管理员

    const GenderUnknown = 0;
    const GenderMale = 1;
    const GenderFemale = 2;

    /** @var null|int[] */
    private $virtualIds = null;

    /**
     * @param string $password
     * @return string
     */
    public static function hashPassword($password)
    {
        $password = empty($password) ? '' : trim($password);

        return md5($password);
    }

    public function __construct($data = null)
    {
        if (empty($data)) {
            $data = array();
        }
        if (empty($data['create_dt'])) {
            $data['create_dt'] = (new \DateTime())->format(QueryBuilder::SqlDateTimeFormat);
        }
        if (empty($data['name'])) {
            $data['name'] = '管理员' . time();
        }
        if (!isset($data['gender'])) {
            $data['gender'] = self::GenderUnknown;
        }

        parent::__construct($data);
    }

    /**
     * @inheritDoc
     */
    public function getConnName()
    {
        return App::$p->adminToMenuDb()->getConnName();
    }

    /**
     * @inheritDoc
     */
    public function getDb($quoted = true)
    {
        return App::$p->adminToMenuDb()->getDb($quoted);
    }

    /**
     * @inheritDoc
     */
    public function getTable($quoted = true)
    {
        return 'admin_user';
    }

    public function formatNormal()
    {
        return $this->format(array(
            'create_dt' => 'createDt',
            'virtual_ids' => false,
        ));
    }

    /**
     * 不走缓存，直接从数据库取数据
     *
     * @param string $phone - 手机号码
     * @return AdminUserEntity|null
     */
    public function findByPhone($phone)
    {
        $phone = empty($phone) ? null : trim($phone);
        if (empty($phone)) {
            return null;
        }

        // 不走缓存
        $result = QueryBuilder::useDb($this->getConnName())->select($this->getTable(), '*', array('phone' => $phone))->getPdoStatement()->fetchAll(\PDO::FETCH_ASSOC);
        if (empty($result)) {
            return null;
        }
        return new AdminUserEntity($result[0]);
    }

    /**
     * 不走缓存，直接从数据库取数据
     *
     * @param string $email - 电子邮箱
     * @return AdminUserEntity|null
     */
    public function findByEmail($email)
    {
        $email = empty($email) ? null : trim($email);
        if (empty($email)) {
            return null;
        }

        // 不走缓存
        $result = QueryBuilder::useDb($this->getConnName())->select($this->getTable(), '*', array('email' => $email))->getPdoStatement()->fetchAll(\PDO::FETCH_ASSOC);
        if (empty($result)) {
            return null;
        }
        return new AdminUserEntity($result[0]);
    }

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->set_('id', $id, self::DataTypeInt);
    }

    /**
     * @return string
     */
    public function getCreateDt()
    {
        return $this->get_('create_dt');
    }

    /**
     * @param string $createDt
     */
    public function setCreateDt($createDt)
    {
        $this->set_('create_dt', $createDt, self::DataTypeString);
    }

    /**
     * @return int[]|null
     */
    public function getVirtualIds()
    {
        if (empty($this->virtualIds)) {
            $virtualIds = $this->get_('virtual_ids');
            if ($virtualIds) {
                $virtualIds = trim($virtualIds);
                if ($virtualIds) {
                    $this->virtualIds = explode(',', $virtualIds);
                }
            }
        }
        return $this->virtualIds;
    }

    /**
     * @param int[]|null $virtualIds
     */
    public function setVirtualIds($virtualIds)
    {
        $this->virtualIds = $virtualIds;

        $validIds = array();
        if ($virtualIds) {
            foreach ($virtualIds as $vid) {
                $vid = empty($vid) ? 0 : intval($vid);
                if ($vid > 0) {
                    $validIds[$vid] = 1;
                }
            }
            if ($validIds) {
                $validIds = array_keys($validIds);
                $validIds = sort($validIds, SORT_NUMERIC);
            }
        }

        $this->set_('virtual_ids', empty($validIds) ? '' : implode(',', $validIds), self::DataTypeString);
    }

    /**
     * @return string
     */
    public function getPhone()
    {
        return $this->get_('phone');
    }

    /**
     * @param string $phone
     */
    public function setPhone($phone)
    {
        $this->set_('phone', $phone, self::DataTypeString);
    }

    /**
     * @return string
     */
    public function getEmail()
    {
        return $this->get_('email');
    }

    /**
     * @param string $email
     */
    public function setEmail($email)
    {
        $this->set_('email', $email, self::DataTypeString);
    }

    /**
     * @return string
     */
    public function getPassword()
    {
        return $this->get_('password');
    }

    /**
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->set_('password', $password, self::DataTypeString);
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->get_('name');
    }

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->set_('name', $name, self::DataTypeString);
    }

    /**
     * @return int
     */
    public function getGender()
    {
        return $this->get_('gender');
    }

    /**
     * @param int $gender
     */
    public function setGender($gender)
    {
        $this->set_('gender', $gender, self::DataTypeInt);
    }

    /**
     * @return string
     */
    public function getAvatar()
    {
        return $this->get_('avatar');
    }

    /**
     * @param string $avatar
     */
    public function setAvatar($avatar)
    {
        $this->set_('avatar', $avatar, self::DataTypeString);
    }
}